﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Xml;
using BLL;
using Model;
namespace UI
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }
        //记住密码
        public void show()
        {
            //获取根节点
            XmlNode node = docum.DocumentElement;
            //判断是否被选中
            if (checkBox2.Checked)
            {
                foreach (XmlNode item in node.ChildNodes)
                {
                    //条件成立就执行
                    if (item["Uid"].InnerText != textBox1.Text)
                    {
                        item["Uid"].InnerText = textBox1.Text;
                        item["Pwd"].InnerText = textBox2.Text;
                        //保存
                        docum.Save("../../XMLFile1.xml");
                    }
                }
            }
            else
            {
                foreach (XmlNode item in node.ChildNodes)
                {
                    //条件成立就执行
                    if (item["Uid"].InnerText == textBox1.Text)
                    {
                        item["Uid"].InnerText = "";
                        item["Pwd"].InnerText = "";
                        //保存
                        docum.Save("../../XMLFile1.xml");
                    }
                }
            }
        }
        //实例化xmldoc对象
        XmlDocument docum = new XmlDocument();
        //当鼠标移到修改密码变色
        private void label4_MouseMove(object sender, MouseEventArgs e)
        {
            label4.ForeColor = Color.Red;
        }
        //当鼠标移开修改密码变色
        private void label4_MouseLeave(object sender, EventArgs e)
        {
            label4.ForeColor = Color.Blue;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ModelAdmins.UserName = textBox1.Text;
            ModelAdmins.UserPWD = textBox2.Text;
            SqlDataReader BLLlof = BLLLoginForm.DengLu();
            if (BLLlof != null)
            {
                while (BLLlof.Read())
                {
                   
                   
                    MessageBox.Show("欢迎前台人员  —  " + BLLlof["UserCompellation"].ToString() + "  —  登录星星餐饮系统");

                    //登录成功调用方法
                    show();
                    主界面 z = new 主界面( BLLlof["UserID"].ToString());
                    this.Hide();
                    z.Show();
                }
                
               
            }
            else
            {
                MessageBox.Show("登录失败,请输入正确的前台密码和账号");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("你确定要退出星星餐饮系统吗？", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.ToString() == "No")
            {
                e.Cancel = true;
            }

        }

        private void label4_Click(object sender, EventArgs e)
        {
            修改密码 x = new 修改密码(textBox1.Text);
            x.ShowDialog();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            //加载xml文件
            docum.Load("../../XMLFile1.xml");
            //获取根节点
            XmlNode node = docum.DocumentElement;
            foreach (XmlNode item in node.ChildNodes)
            {
                //条件成立就执行
                if (item["Uid"].InnerText != "")
                {
                    textBox1.Text = item["Uid"].InnerText;
                    textBox2.Text = item["Pwd"].InnerText;
                    checkBox2.Checked = true;
                }
            }
        }
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                textBox2.UseSystemPasswordChar = false;
            }
            else
            {
                textBox2.UseSystemPasswordChar = true;
            }
        }
    }
}
